package cn.com.generaldata.os.service.indicator_evaluation.industry_layout_analysis;

import org.apache.commons.lang3.Validate;

import cn.com.generaldata.jsme_indicator_system.entity.IndicatorEvaluation;
import cn.com.generaldata.jsme_indicator_system.util.Constants;
import cn.com.generaldata.os.service.indicator_constant.ProvinceIndicatorConstant;
import cn.com.generaldata.os.service.indicator_evaluation.IndicatorEvaluationCalculateService;
import cn.com.generaldata.os.util.BeanMapper;
import cn.com.generaldata.os.vo.IndicatorAccountVO;

/**
 * 主要海洋产业集中系数指标计算类.
 */
public class MajorMarineIndustryConcentFactorCalculateService extends IndicatorEvaluationCalculateService {

	private String cityPopulationIndicatorName = ProvinceIndicatorConstant.OCEAN_PERSON_NAME;

	@Override
	protected Double calculate(IndicatorEvaluation indicator, IndicatorAccountVO indicatorAccountVO) {
		Double result = null;
		if (indicator.getSection() == Constants.INDICATOR_LEVEL_PROVINCE) {

		} else if (indicator.getSection() == Constants.INDICATOR_LEVEL_CITY) {
			result = calculateCityResult(indicator, indicatorAccountVO);
		}
		return result;

	}

	/**
	 * 计算市级集中系数
	 * 
	 * @param indicatorAccountVO
	 * @return
	 */
	private Double calculateCityResult(IndicatorEvaluation indicator, IndicatorAccountVO indicatorAccountVO) {

		Double addValueOfIndustry = getAddValueOfIndustry(indicatorAccountVO, indicator);

		Double parentAddValueOfIndustry = getParentAddValueOfIndustry(indicatorAccountVO, indicator);

		Double cityPopulation = getCityPopulation(indicatorAccountVO);

		double cityPopulationSum = 0d;
		for (String areaId : ProvinceIndicatorConstant.CITYS) {
			IndicatorAccountVO vo = BeanMapper.map(indicatorAccountVO, IndicatorAccountVO.class);
			vo.setAreaId(areaId);
			cityPopulationSum += getCityPopulation(vo);
		}
		return (addValueOfIndustry / cityPopulation) / (parentAddValueOfIndustry / cityPopulationSum);
	}

	private Double getParentAddValueOfIndustry(IndicatorAccountVO indicatorAccountVO, IndicatorEvaluation indicator) {

		IndicatorAccountVO parentVO = BeanMapper.map(indicatorAccountVO, IndicatorAccountVO.class);
		// 查找当前区划的上级区划
		String areaId = indicatorAccountVO.getAreaId();
		parentVO.setAreaId(getAreaParentId(areaId));

		return getAddValueOfIndustry(parentVO, indicator);
	}

	private Double getAddValueOfIndustry(IndicatorAccountVO indicatorAccountVO, IndicatorEvaluation indicator) {
		String indicatorName = ProvinceIndicatorConstant.MAJOR_MARINE_INDUSTRY_ADD_VALUE_MAP.get(indicator
				.getIndustryCode());
		Double result = getInputOrAccountingIndicatorValue(indicatorAccountVO, indicatorAccountVO.getYear(),
				indicatorName);
		Validate.isTrue(result != null);
		return result;
	}

	/**
	 * 获得城市人口数量
	 * 
	 * @param indicatorAccountVO
	 * @return
	 */
	private Double getCityPopulation(IndicatorAccountVO indicatorAccountVO) {
		Double result = getInputOrAccountingIndicatorValue(indicatorAccountVO, indicatorAccountVO.getYear(),
				cityPopulationIndicatorName);
		Validate.isTrue(result != null);
		return result;
	}

}
